import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/views/Layout.vue'
import Dashboard from '@/views/Dashboard.vue'
// import DashboardSimple from '@/views/Dashboard.vue'
import ClientManagement from '@/views/ClientManagement.vue'
import RemoteControl from '@/views/RemoteControl.vue'


// 客户端管理相关
import ClientConfig from '@/views/ClientConfig.vue'
import ScreenSharing from '@/views/ScreenSharing.vue'



// 预警中心相关
import AlertList from '@/views/AlertList.vue'
import AlertReport from '@/views/AlertReport.vue'

// 行为监控相关
import BehaviorAnalysis from '@/views/BehaviorAnalysis.vue'
import RecordingManagement from '@/views/RecordingManagement.vue'
import ScreenshotManagement from '@/views/ScreenshotManagement.vue'

// 摄像头设备管理相关
import CameraDeviceManagement from '@/views/CameraDeviceManagement.vue'
import CameraAlertManagement from '@/views/CameraAlertManagement.vue'
import CameraAlgorithmConfig from '@/views/CameraAlgorithmConfig.vue'
import CameraVideoPreview from '@/views/CameraVideoPreview.vue'


// 视频智能分析相关



// 系统设置相关
import UserDeptManagement from '@/views/UserDeptManagement.vue'

import DeploymentManagement from '@/views/DeploymentManagement.vue'

import Login from '@/views/Login.vue'


const routes = [
  // 登录页面
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录',
      requiresAuth: false  // 登录页面不需要认证
    }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/client/list',
    meta: { requiresAuth: true },  // 主布局需要认证
    children: [

      {
        path: 'client/list',
        name: 'ClientManagement',
        component: ClientManagement,
        meta: { title: '客户端配置', icon: 'Monitor' }
      },
      {
        path: 'client/config',
        name: 'ClientConfig',
        component: ClientConfig,
        meta: { title: '管理规则配置', icon: 'Setting' }
      },


      {
        path: 'alert/list',
        name: 'AlertList',
        component: AlertList,
        meta: { title: '预警列表', icon: 'Warning' }
      },
      {
        path: 'alert/report',
        name: 'AlertReport',
        component: AlertReport,
        meta: { title: '预警统计报表', icon: 'Document' }
      },
      {
        path: 'alert/report2',
        name: 'AlertReport2',
        component: Dashboard,
        meta: { title: '预警实时看板', icon: 'Document' }
      },
      {
        path: 'behavior/analysis',
        name: 'BehaviorAnalysis',
        component: BehaviorAnalysis,
        meta: { title: '鼠标键盘操作趋势分析', icon: 'TrendCharts' }
      },
      {
        path: 'behavior/recording',
        name: 'RecordingManagement',
        component: RecordingManagement,
        meta: { title: '录屏记录管理', icon: 'VideoCamera' }
      },
      {
        path: 'behavior/screenshot',
        name: 'ScreenshotManagement',
        component: ScreenshotManagement,
        meta: { title: '截屏记录管理', icon: 'Picture' }
      },

      {
        path: 'behavior/screen-sharing',
        name: 'ScreenSharing',
        component: ScreenSharing,
        meta: { title: '屏幕共享', icon: 'VideoCamera' }
      },

      // 摄像头设备管理路由
      {
        path: 'camera/device',
        name: 'CameraDeviceManagement',
        component: CameraDeviceManagement,
        meta: { title: '设备管理', icon: 'Monitor' }
      },
      {
        path: 'camera/alert',
        name: 'CameraAlertManagement',
        component: CameraAlertManagement,
        meta: { title: '报警管理', icon: 'Warning' }
      },
      {
        path: 'camera/algorithm',
        name: 'CameraAlgorithmConfig',
        component: CameraAlgorithmConfig,
        meta: { title: '算法配置', icon: 'Setting' }
      },
      {
        path: 'camera/video',
        name: 'CameraVideoPreview',
        component: CameraVideoPreview,
        meta: { title: '视频预览', icon: 'VideoCamera' }
      },


      {
        path: 'settings/user',
        name: 'UserDeptManagement',
        component: UserDeptManagement,
        meta: { title: '用户与部门管理', icon: 'User' }
      },

      {
        path: 'settings/deployment',
        name: 'DeploymentManagement',
        component: DeploymentManagement,
        meta: { title: '安装部署管理', icon: 'Download' }
      },


      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/Profile.vue'),
        meta: { title: '个人资料', icon: 'User' }
      },
      {
        path: 'change-password',
        name: 'ChangePassword',
        component: () => import('@/views/ChangePassword.vue'),
        meta: { title: '修改密码', icon: 'Lock' }
      }
    ]
  },
  {
    path: '/control/:clientId',
    name: 'RemoteControl',
    component: RemoteControl,
    meta: { title: '远程控制' }
  },

]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查路由是否需要认证
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth !== false)
  const token = localStorage.getItem('auth_token')

  if (requiresAuth && !token) {
    // 需要认证但没有token，跳转到登录页
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
  } else if (to.path === '/login' && token) {
    // 已登录用户访问登录页，跳转到首页
    next('/')
  } else {
    // 正常访问
    next()
  }
})

export default router
